<?php
/**
 * 专项基金广宣传播
 * @author Administrator
 *
 */
class SpecialOnline extends MY_Controller{
    function SpecialOnline() {
        parent::MY_Controller();
    }
    /**
     * 添加或修改
     */
    function add(){
        $this->load->model('specialOnlineModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId' => $this->user['userId'],
                'procId' => (int)$this->input->post('procId'),
                'projectName' => $this->input->post('projectName'),
                'mediaType' => (int)$this->input->post('mediaType'),
                'mediaTypeAlias' => $this->input->post('mediaTypeAlias'),
                'mediaName' => $this->input->post('mediaName'),
                'mediaContent' => $this->input->post('mediaContent'),
                'startTime' => $this->input->post('startTime'),
                'endTime' => $this->input->post('endTime'),
                'budgetApply' => (float)$this->input->post('budgetApply'),
                'budgetFileId' => (int)$this->input->post('budgetFileId'),
                'remarks' => $this->input->post('remarks'),
                'voucher' => serialize(array()),
            );
            $data['storeId'] || showError('请选择所属专营店ID');
            $data['procId'] || showError('请选择所属主线流程');
            $data['projectName'] || showError('请输入项目名称');
            $data['mediaType'] || showError('请选择广宣形式');
            if( $data['mediaType'] == -1 && !$data['mediaTypeAlias']){
                showError('请输入其他广宣形式');
            }
            $data['mediaName'] || showError('请输入投放媒体');
            $data['mediaContent'] || showError('请输入投入内容');
            $data['startTime'] || showError('请选择开始时间');
            $data['endTime'] || showError('请选择结束时间');
            strtotime($data['startTime']) <= strtotime($data['endTime']) || showError('开始时间必须早于结束时间');
            $data['budgetApply'] > 0 || showError('请输入预算申请金额');
            $data['budgetFileId'] || showError('请上传广宣传播方案');
            
            if ($id){
                $findData = $this->specialOnlineModel->getData($id);
                if (in_array($findData['budgetAuditRs'], array(0,3))){
                    $data['budgetAuditRole'] = 0;
                    $data['budgetAuditRs'] = 0;
                }
                if ($this->specialOnlineModel->edit(array('id'=>$id,'storeId'=>$data['storeId']), $data)){
                    showSuccess('编辑成功');
                }else{
                    showError('编辑失败');
                }
            }else{
                $data['createTime'] = time();
                if ($this->specialOnlineModel->add($data)){
                    showSuccess('添加成功');
                }else{
                    showError('添加失败');
                }
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $id = (int)$this->input->get('id');
            if ($id){
                $onlineData = $this->specialOnlineModel->getData($id);
                $this->viewData['onlineData'] = $onlineData;
                $procId = (int)$onlineData['procId'];
            }
            $this->load->model('specialProcessModel');
            $procData = $this->specialProcessModel->getData($procId);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            $this->load->view('SpecialOnline/add', $this->viewData);
        }
    }
    /**
     * 删除
     */
    function del(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的广宣传播');
        $this->load->model('specialOnlineModel');
        if($this->specialOnlineModel->del(array('id'=>$id, 'storeId'=>$this->user['userId']))){
            showSuccess('删除广宣传播成功');
        }else{
            showError('删除广宣传播失败');
        }
    }
    /**
     * 结算申请
     */
    function settleEdit(){
        $this->load->model('specialOnlineModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $id || showError('请选择要申请结算的广宣传播');
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择主线流程数据');
            $data = array(
                'callCount' => (int)$this->input->post('callCount'),
                'comeCount' => (int)$this->input->post('comeCount'),
                'settleApply' => (float)$this->input->post('settleApply'),
                'settleFileId' => (int)$this->input->post('settleFileId'),
                'settleSubTime' => time(),
                'settleAuditRole' => 0,
                'settleAuditRs' => 0,
            );
            $data['callCount'] || showError('请输入来电量');
            $data['comeCount'] || showError('请输入到店量');
            $data['settleApply'] || showError('请输入结算申请费用');
            $data['settleFileId'] || showError('请上传结算材料');
            /*$this->load->model('fileModel');
            $voucherFiles = isset($_POST['voucher']) ? (array)$_POST['voucher'] : array();
            $voucherList = $this->fileModel->getFileDataByViewPaths($voucherFiles);
            count($voucherList) >= 5 || showError('至少上传5张凭证照片');
            $data['voucher'] = serialize(array_values($voucherList));*/
            if ($this->specialOnlineModel->edit(array('id'=>$id,'storeId'=>$this->user['userId']),$data)){
                //更新主线流程结算审核状态
                $this->load->model('specialProcessModel');
                $this->specialProcessModel->settleUpdateStatus($procId);
                showSuccess('结算申请成功');
            }else{
                showError('结算申请失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            $id || showError('请选择要申请结算的广宣传播');
            $onlineData = $this->specialOnlineModel->getNewData(array('id'=>$id,'storeId'=>$this->user['userId']));
            $this->viewData['onlineData'] = $onlineData;
            $this->load->view('SpecialOnline/settleEdit', $this->viewData);
        }
    }
    /**
     * 广宣传播凭证查看
     */
    function voucherCheck(){
        $id = (int)$this->input->get('id');
        $id || showError('请选择要查看凭证的广宣传播');
        $this->load->model('specialOnlineModel');
        $onlineData = $this->specialOnlineModel->getNewData($id);
        $this->viewData['voucher'] = $onlineData['voucher'];
        $this->load->view('common/voucherCheck', $this->viewData);
    }
}